<?php 
class fileUploadAction extends AdminMasterUploadControl{
	public function execute($request){
		//check authentiaction
		$directory_path  	= $request->getParameter('path','');
		$filter				= $request->getParameter('filter','');
		$url = 'path='.$directory_path.'&filter='.$filter;
		$is_error = 0; // no thing
		if($directory_path=='all') $directory_path = '';
		if(!empty($filter) && $filter!='*'){
			$filter_arr = explode('|',$filter);
			$sepa ='';
			$filter = '';
			foreach($filter_arr as $val){
				if(strpos(sfConfig::get('app_uploadControl_allowed'), strtolower($val))!==false){
					$filter.=$sepa.$val;
					$sepa = '|';
				}
			}
		}else{
			$filter = sfConfig::get('app_uploadControl_allowed');
		}
		if($request->isMethod('POST')){
			$name = basename($_FILES['file_upload']['name']);
			$arr_ext = explode(".", $name);
			$ext = $arr_ext[count($arr_ext)-1];
			if(strpos($filter, strtolower($ext))!==false){
				$path = sfConfig::get('sf_upload_dir').'/'.$directory_path.'/'.$name;
				if(file_exists($path)){
					$path = sfConfig::get('sf_upload_dir').'/'.$directory_path.'/'. str_replace('.', '_'.time().'.', $name);
				}
				if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $path)){
					chmod($path, 0777);
					$is_error = 1; //upload success
				}
			}else{
				$is_error = 2; // type mismatch;
			}
		}
		$this->setVar('is_error', $is_error);
		$this->setVar('url', $url);
	}
}